package com.whu.waterworks.impl;

import java.sql.ResultSet;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.HibernateTemplate;

import com.whu.waterworks.dao.RealWaterPlantDao;
import com.whu.waterworks.dto.WaterPlantDto;
import com.whu.waterworks.model.TWaterplantdata;

public class RealWaterPlantDaoImpl implements RealWaterPlantDao{

	private HibernateTemplate hibernateTemplate;
	
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	@Override
	public List<TWaterplantdata> getAll() {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		List<TWaterplantdata> waterplantdatas = (List<TWaterplantdata>)hibernateTemplate.find("from TWaterplantdata _TWaterplantdata ");
		if(waterplantdatas!=null && waterplantdatas.size()>0)
			return waterplantdatas;
		return null;
	}

	@Override
	public List<TWaterplantdata> getWaterplantdatasById(String code) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		List<TWaterplantdata> waterplantdatas = (List<TWaterplantdata>)hibernateTemplate.find("from TWaterplantdata _TWaterplantdata where _TWaterplantdata.id.waterPlantNum='"+code+"' order by _TWaterplantdata.id.time desc");
		if(waterplantdatas!=null && waterplantdatas.size()>0)
			return waterplantdatas;
		return null;
	}

	@Override
	public TWaterplantdata getLastData(String code) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		List<TWaterplantdata> waterplantdatas = (List<TWaterplantdata>)hibernateTemplate.find("from TWaterplantdata _TWaterplantdata where _TWaterplantdata.id.waterPlantNum='"+code+"' order by _TWaterplantdata.id.time desc");
		if(waterplantdatas!=null && waterplantdatas.size()>0)
			return waterplantdatas.get(0);
		return null;
	}

	@Override
	public List<WaterPlantDto> getWaterplantdatasByIdType(String code,
			String type) {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		List<WaterPlantDto> waterPlantDtos=(List<WaterPlantDto>)hibernateTemplate.find("select new WaterPlantDto(t.id.waterPlantNum,t.id.time,t."+type+") from TWaterplantdata t where t.id.waterPlantNum='"+code+
				"' order by t.id.time desc");
		if(waterPlantDtos!=null && waterPlantDtos.size()>0)
			return waterPlantDtos;
		return null;
	}


}
